﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Globalization;
using System.Web.Security;

namespace MvcApplicationDataHound.Models
{
    #region ENUMs
    public enum TSD_PreparationStateCode
    {
        PREPARED,
        UNPREPARED
    }

    public enum imageTypeCode
    {
        LOGO,
        OUT_OF_PACKAGE_IMAGE,
        PRODUCT_IMAGE,
        PRODUCT_LABEL_IMAGE
    }

    public enum TSD_ProductInformationTypeCode
    {
        AUDIO,
        CONSUMER_HANDLING_AND_STORAGE,
        DOCUMENT,
        IFU,
        MARKETING_INFORMATION,
        OTHER_EXTERNAL_INFORMATION,
        VIDEO,
        WARRANTY_INFORMATION,
        WEBSITE
    }

    public enum MeasurementPrecision
    {
        APPROXIMATELY,
        EXACT,
        LESS_THAN
    }
    #endregion

    #region ProductDescrioptions
    public class Description70
    {
        [Required(ErrorMessage = "Description70 Description is required")]
        string Description { get; set; }

        [Required(ErrorMessage = "Description70 LanguageCode is required")]
        string LanguageCode { get; set; }
    }

    public class Description80
    {
        [Required(ErrorMessage = "Description80 Description is required")]
        string Description { get; set; }

        [Required(ErrorMessage = "Description80 LanguageCode is required")]
        string LanguageCode { get; set; }
    }

    public class Description2500
    {
        [Required(ErrorMessage = "Description2500 Description is required")]
        string Description { get; set; }

        [Required(ErrorMessage = "Description2500 LanguageCode is required")]
        string LanguageCode { get; set; }
    }
    #endregion

    public class ProductService
    {
        public TSD_ProductData GetProdcutData(double SearchedUpc)
        {
            var dataAccess = new DataAccess.TSD_DataAccess();

            var ResultTbl = (dataAccess.GetTsdProduct(SearchedUpc)).Tables[0];
            var productData = new TSD_ProductData();

            foreach (DataRow row in ResultTbl.Rows)
            {
                productData.Gtin = Convert.ToInt32(row["Gtin"]);
                productData.TargetMarket = Convert.ToInt32(row["TargetMarket"]);
                if (row["InformationProviderGln"].ToString() != String.Empty)
                {
                    productData.InformationProviderGln = Convert.ToInt32(row["InformationProviderGln"]);
                }
                productData.InformationProviderName = row["InformationProviderName"].ToString();
                productData.TimeToLive = row["TimeToLive"].ToString();
            }

            return productData;
        }
    }
    
    public class TSD_ProductData
    {
        public int SearchUpc { get; set; }

        //Control Number
        [Key]
        [Required(ErrorMessage = "gtin Control Number is required")]
        public int Gtin { get; set; }

        //CountryCode
        [StringLength(3)]
        public int? TargetMarket { get; set; }

        [StringLength(13)]
        public double? InformationProviderGln { get; set; }

        public string InformationProviderName { get; set; }

        //Duration (End/Start) of product info validity
        public string TimeToLive { get; set; }

        //PK to TSD_ProductDataRecord
        public TSD_ProdcutDataRecord[] ProductDataRecord { get; set; }

        //version control
        public AttributeValuePair[] AvpList { get; set; }
    }

    public class TSD_BasicProductInformationModule
    {
        //Customer Friendly Names
        [Required(ErrorMessage = "TSD_BasicProductInformationModule productName is required")]
        public Description80[] productName { get; set; }

        //Customer Friends Descriptions
        public Description2500 consumerMarketingDescription { get; set; }

        [StringLength(8)]
        public string[] gpcCategoryCode { get; set; }

        [Required(ErrorMessage = "TSD_BasicProductInformationModule brandNameInformation is required")]
        public TSD_BrandNameInformation brandNameInformation { get; set; }

        public TSD_ProductInformationLink[] productInformationLink { get; set; }

        public TSD_ImageLink[] imageLink { get; set; }

        public AttributeValuePair avpList { get; set; }
    }

    public class TSD_BrandNameInformation
    {
        [Required(ErrorMessage = "TSD_BrandNameInformation brandName is required")]
        public string BrandName { get; set; }

        public Description70[] LanguageSpecificBrandName { get; set; }
    }

    public class TSD_ProductInformationLink
    {
        [Required(ErrorMessage = "TSD_ProductInformationLink URI is required")]
        public AnyURI Uri { get; set; }

        [Required(ErrorMessage = "TSD_ProductInformationLink productInformationTypeCode is required")]
        public TSD_ProductInformationTypeCode productInformationTypeCode { get; set; }

        public LanguageCode[] LanguageCode { get; set; }

        public AttributeValuePair AvpList { get; set; }
    }

    public class TSD_NutritionalProductInformationModule
    {
        [Required(ErrorMessage = "TSD_NutritionalProductInformationModule nutrientHeader is required")]
        public TSD_NutrientHeader[] NutrientHeader { get; set; }

        public AttributeValuePair AvpList { get; set; }
    }

    public class TSD_NutrientHeader
    {
        [Required(ErrorMessage = "TSD_NutrientHeader preparationStateCode is required")]
        public TSD_PreparationStateCode PreparationStateCode { get; set; }

        public Description70[] DailyValueIntakeReference { get; set; }

        public Description70[] ServingSizeDescription { get; set; }

        public Measurement[] ServingSize { get; set; }

        public Measurement[] NutrientBasisQuantity { get; set; }

        public Decimal NumberOfServings { get; set; }

        [Required(ErrorMessage = "TSD_NutrientHeader nutrientDetail is required")]
        public TSD_NutrientDetail[] NutrientDetail { get; set; }

        public AttributeValuePair AvpList { get; set; }
    }

    public class TSD_NutrientDetail
    {
        [Required(ErrorMessage = "TSD_NutrientDetail nutrientTypeCode is required")]
        public TSD_NutrientTypeCode NutrientTypeCode { get; set; }

        public float DailyValueIntakePercent { get; set; }

        public MeasurementPrecision MasurementPrecision { get; set; }

        public Measurement QuantityContained { get; set; }

        public AttributeValuePair AvpList { get; set; }
    }

    public class TSD_NutrientTypeCode
    {

    }

    public class TSD_ImageLink
    {
        [Required(ErrorMessage = "TSD_ImageLink URI is required")]
        public AnyURI Uri { get; set; }

        [Required(ErrorMessage = "TSD_ImageLink imageTypeCode is required")]
        public TSD_imageTypeCode ImageTypeCode { get; set; }

        public LanguageCode[] LanguageCode { get; set; }

        public uint? ImagePixelHeight { get; set; }

        public uint? ImagePixelWidth { get; set; }

        public Measurement FileSize { get; set; }

        public AttributeValuePair AvpList { get; set; }
    }

    public class TSD_imageTypeCode
    {
        public string ImageTypeCode { get; set; }
    }

    public class Measurement
    {
        public float MeasurementValue { get; set; }

        public string MeasurementType { get; set; }
    }

    public class AnyURI
    {
        public string Uri { get; set; }
    }

    public class TSD_ProdcutDataRecord
    {
        public Description70 ProductionVariantDescription { get; set; }
        public Description70 ProductionVariantEffectiveDataTime { get; set; }
        ///public module
        public AttributeValuePair AttributeValue { get; set; }
    }

    public class LanguageCode
    {
        //Must be lowercase
        [StringLength(2)]
        public string Language { get; set; }
    }

    public class AttributeValuePair
    {
        [Required(ErrorMessage = "AttributeValuePair AttribName is required")]
        public string AttribName { get; set; }
        [Required(ErrorMessage = "AttributeValuePair AttribValue is required")]
        public string AttribValue { get; set; }
    }
}